package com.xoa.service.mealmanage;

import com.xoa.dao.mealmanage.MealManageMapper;
import com.xoa.model.mealmanage.MealManage;
import com.xoa.model.users.Users;
import com.xoa.service.department.DepartmentService;
import com.xoa.service.users.UsersService;
import com.xoa.util.CookiesUtil;
import com.xoa.util.ToJson;
import com.xoa.util.common.StringUtils;
import com.xoa.util.common.session.SessionUtils;
import com.xoa.util.page.PageParams;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import javax.servlet.http.Cookie;
import javax.servlet.http.HttpServletRequest;
import java.util.Date;
import java.util.List;
import java.util.Map;

@Service
public class MealManageService {

    @Resource
    private MealManageMapper mealManageMapper;

    @Resource
    private UsersService usersService;

    @Resource
    private DepartmentService departmentService;

    /**
     * 作者 廉立深
     * 日期 2020/10/23
     * 方法介绍  
     * 参数 [request, map, mealManage]
     * 返回 com.xoa.util.ToJson
     **/
    public ToJson getMealMap(HttpServletRequest request, Map map, MealManage mealManage) {
        ToJson toJson = new ToJson(1,"false");
        try {
            Cookie redisSessionCookie = CookiesUtil.getCookieByName(request, "redisSessionId");
            Users users = SessionUtils.getSessionInfo(request.getSession(), Users.class, new Users(),redisSessionCookie);

            if(!StringUtils.checkNull(mealManage.getUserName())){
                map.put("userId",users.getUserId());
            }


            map.put("mealId",mealManage.getMealId());
            map.put("mealType",mealManage.getMealType());
            map.put("college",mealManage.getCollege());
            //时间
            map.put("stadate",request.getParameter("stadate"));
            map.put("enddate",request.getParameter("enddate"));

            //申请人  和   部门
            if (!StringUtils.checkNull(mealManage.getUserId())){
                map.put("userId",mealManage.getUserId());
            }
            map.put("deptId",mealManage.getDeptId());

            List<MealManage> mealMapList = mealManageMapper.getMealMap(map);
            mealMapList.forEach( item ->{
                //用户名
                item.setUserName(usersService.getUserNameById(item.getUserId()));
                //部门
                item.setDeptName(departmentService.getDpNameById(item.getDeptId()));
            });
            toJson.setObj(mealMapList);
            PageParams pageParams = (PageParams) map.get("pageParams");
            toJson.setTotleNum(pageParams.getTotal());
            toJson.setFlag(0);
            toJson.setMsg("ok");
        }catch (Exception e){
            e.printStackTrace();
            toJson.setMsg(e.getMessage());
        }
        return toJson;
    }

    /**
     * 作者 廉立深
     * 日期 2020/10/23
     * 方法介绍  编辑用餐接口
     * 参数 [request, mealManage]
     * 返回 com.xoa.util.ToJson
     **/
    public ToJson editMealMap(HttpServletRequest request, MealManage mealManage) {
        ToJson toJson = new ToJson(1,"false");
        try {
            if ( mealManage.getMealId() != null ){
                mealManageMapper.updateByPrimaryKeySelective(mealManage);
            }else{
                mealManageMapper.insertSelective(mealManage);
            }
            toJson.setFlag(0);
            toJson.setMsg("ok");
        }catch (Exception e){
            e.printStackTrace();
            toJson.setMsg(e.getMessage());
        }
        return toJson;
    }

    /**
     * 作者 廉立深
     * 日期 2020/10/23
     * 方法介绍  删除接口
     * 参数 [request, mealManage]
     * 返回 com.xoa.util.ToJson
     **/
    public ToJson delMealId(HttpServletRequest request, MealManage mealManage) {
        ToJson toJson = new ToJson(1,"false");
        try {
            toJson.setObject(mealManageMapper.deleteByPrimaryKey(mealManage.getMealId()));
            toJson.setFlag(0);
            toJson.setMsg("ok");
        }catch (Exception e){
            e.printStackTrace();
            toJson.setMsg(e.getMessage());
        }
        return toJson;
    }
}
